Skip to content

REF: Remove side-effects from importing Styler #52995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

rhshadrach
Copy link
Member

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

I initially pursued this because I thought it was necessary for #27522, but realize now Styler isn't defined in core. However it would be necessary for #52994 if we decide to go that route.

The "cached classmethods" are a bit of a hack. It works because when you set e.g. StylerRenderer.template_string = some_value, you're just overwriting the entire property with the value. Still looking for a better way to do this.

@rhshadrach rhshadrach added Refactor Internal refactoring of code Dependencies Required and optional dependencies Styler conditional formatting using DataFrame.style labels Apr 29, 2023
@attack68
Copy link
Contributor

attack68 commented May 1, 2023

What about pushing the loading of templates into the render methods themselves. The class level parameters could just be strings defining the default template file location? Maybe keep the cached env loader.

Currently we have

def _render_html(...) -> str:
        d = self._render(sparse_index, sparse_columns, max_rows, max_cols, " ")
        d.update(kwargs)
        return self.template_html.render(
            **d,
            html_table_tpl=self.template_html_table,
            html_style_tpl=self.template_html_style, 
        )

then perhaps ..

class StyleRender:
      template_html = "some_string_path"
      ...


def _render_html(...) -> str:
        d = self._render(sparse_index, sparse_columns, max_rows, max_cols, " ")
        d.update(kwargs)
        template_html = self.env.get_template(self.template_html)
        template_html_table = self.env.get_template(self.template_html_table)
        template_html_style = self.env.get_template(self.template_html_style)
        return template_html.render(
            **d,
            html_table_tpl=template_html_table, 
            html_style_tpl=template_html_style, 
        )

@rhshadrach
Copy link
Member Author

@attack68: finally had some time to look into this. Your suggestion would make it so that users can no longer modify the templates. Is that a breaking change?

@attack68
Copy link
Contributor

Creating modified templates is documented as:

class MyStyler(Styler):
    env = Environment(
        loader=ChoiceLoader([
            FileSystemLoader("templates"),  # contains ours
            Styler.loader,  # the default
        ])
    )
    template_html_table = env.get_template("myhtml.tpl")

where myhtml.tpl is a custom template file in a user directory.

It is my understanding that the native template files stored in the python environment are not really modifiable or suitable to be modified, but a user has to add custom templates which might refer to them, as is the case in the documentation.

Im sure it is possible to create a more user friendly way of doing this which can still avoid your caching.

@rhshadrach
Copy link
Member Author

Im sure it is possible to create a more user friendly way of doing this which can still avoid your caching.

Are you interested in taking this up?

@attack68
Copy link
Contributor

consider #53429

@github-actions
Copy link
Contributor

This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label Jun 28, 2023
@rhshadrach rhshadrach closed this Jun 28, 2023
@rhshadrach rhshadrach deleted the add_styler_to_typing_api branch September 27, 2023 21:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dependencies Required and optional dependencies Refactor Internal refactoring of code Stale Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants